import java.util.Objects;
import java.util.Scanner;
import java.util.Stack;

public class Calculator {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入一个算术表达式: ");
        String expression = scanner.nextLine();
        double result = calculate(expression);
        System.out.println("计算结果为: " + result);
    }

    private static double calculate(String expression) {
        Stack<Double> numberStack = new Stack<>();
        Stack<Character> operatorStack = new Stack<>();
        Stack<Character> bracket = new Stack<>();
        int cnt = 0;
        for (int i = 0; i < expression.length(); i++) {
            char ch = expression.charAt(i);
            if (ch == ' ') {
                // 忽略空格
            } else if (Character.isDigit(ch)) {
                // 如果是数字，一直读取到数字的结尾，并将数字压入数字栈
                //将字符串转化为浮点数
                //双指针
                int j = i;
                while (j < expression.length() && (Character.isDigit(expression.charAt(j)) || expression.charAt(j) == '.')) {
                    j++;
                    //双指针之间截取出来一个数字 可以是浮点数
                }
                double num = Double.parseDouble(expression.substring(i, j));
                //将i，j之间的数字截取出来并转化为浮点数
                numberStack.push(num);
                //入栈
                i = j - 1;
                //跳过这个数字
            } else if (ch == '+' || ch == '-' || ch == '*' || ch == '/') {
                // 如果是运算符，判断栈顶运算符优先级是否大于等于该运算符，如果是，则弹出栈顶运算符和两个数进行计算，将结果压入数字栈
                // 否则将运算符压入运算符栈
                while (!operatorStack.isEmpty() && priority(operatorStack.peek()) >= priority(ch)) {
                    char operator = operatorStack.pop();
                    double num2 = numberStack.pop();
                    double num1 = numberStack.pop();
                    double result = operate(num1, num2, operator);
                    numberStack.push(result);
                }
                operatorStack.push(ch);
            } else if (ch == '(' || ch == '[' || ch == '{' ) {
                bracket.push(ch);
            }else if(ch == ')' || ch == ']' || ch == '}'){
                while (!bracket.isEmpty() ){
                    bracket.pop();
                    char operator = operatorStack.pop();
                    double num2 = numberStack.pop();
                    double num1 = numberStack.pop();
                    double result = operate(num1, num2, operator);
                    numberStack.push(result);
                }
                bracket.push(ch);
            } else {
                // 非法字符
                throw new IllegalArgumentException("输入包含非法字符");
            }
        }

        while (!operatorStack.isEmpty()) {
            char operator = operatorStack.pop();
            double num2 = numberStack.pop();
            double num1 = numberStack.pop();
            double result = operate(num1, num2, operator);
            numberStack.push(result);
        }

        return numberStack.pop();
    }

    private static int priority(char operator) {
        //优先级判断
        //乘除法优先级大于加减法
        switch (operator) {
            case '+':
            case '-':
                return 1;
            case '*':
            case '/':
                return 2;
            default:
                throw new IllegalArgumentException("不支持的运算符: " + operator);
        }
    }

    private  static  int bracketPriority(char bracket){
        switch (bracket){
            case '(':
            case ')':
                return 1;
            case ']':
            case '[':
                return 2;
            case '{':
            case '}':
                return 3;
            default:
                throw new IllegalArgumentException("括号错误");
        }
    }

//    private static int allPriority(char symble){
//        switch (symble){
//            case '+':
//            case '-':
//            case '*':
//            case '/':
//                return 1;
//            case '(':
//            case ')':
//            case '[':
//            case ']':
//            case '{':
//            case '}':
//                return 2;
//        }
//    }

    private static double operate(double num1, double num2, char operator) {
        //加减乘除逻辑
        switch (operator) {
            case '+':
                return num1 + num2;
            case '-':
                return num1 - num2;
            case '*':
                return num1 * num2;
            case '/':
                return num1 / num2;
            default:
                throw new IllegalArgumentException("不支持的运算符: " + operator);
        }
    }
}
